package controller;

import dao.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class AdminController {
    @Autowired
    private UserDAO userDao;

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public String registration(ModelMap model) {
        model.addAttribute("admins", userDao.getUsersByRole("ROLE_ADMIN"));
        model.addAttribute("users", userDao.getUsersByRole("ROLE_USER"));
        return "/admin";
    }

    @RequestMapping(value = "/adminactions", method = RequestMethod.GET)
    public String registration(@RequestParam("name") String name, @RequestParam("action") String action
    ) {
        if (name != null) {
            if (action.contentEquals("upgrade"))
                userDao.update(name, "ROLE_ADMIN");
            if (action.contentEquals("delete"))
                userDao.delete(name);
        }
        return "/adminactions";
    }
}
